/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;

/**
 * yangzhongshan
 *
 * @since 2021-03-23
 */
public class TitleBarComponent extends DirectionalLayout {
    private Text titileText;
    private Image imgbace;
    private Context context;
//    private String title = "";
    private AbilitySlice abilitySlice = null;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public TitleBarComponent(Context context) {
        super(context);
        this.context = context;
        initview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 1
     */
    public TitleBarComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        initview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 1
     * @param styleName 1
     */
    public TitleBarComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        initview();
    }

    private void initview() {
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_basecompent_layout, this, true);
        titileText = (Text) findComponentById(ResourceTable.Id_bace_titile);
        imgbace = (Image) findComponentById(ResourceTable.Id_imgbace);
        imgbace.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (abilitySlice != null) {
                    abilitySlice.terminate();
                } else {
                    context.terminateAbility();
                }
            }
        });
    }

    /**
     * 设置标题
     *
     * @param name 标题
     */
    public void setTitile(String name) {
        String tl = name.length() < 1 ? "关于" : name;
        titileText.setText(tl);
    }

    public void setAbility(AbilitySlice abSlice) {
        this.abilitySlice = abSlice;
    }
}
